package online.shenme.day07.test04;


import java.util.Arrays;

public class NewListImpl<E> implements NewList<E> {
    private Object[] list = new Object[0];
    private int count;

    @Override
    public Object[] add(E e) {
        return add(e, count);
    }

    private Object[] add(E e, int index) {
        if (count < list.length) {
            list[index] = e;
            count++;
        } else {
            list = grow(index);
            add(e, index);
        }
        return list;
    }

    private Object[] grow(int index) {
        if (list.length != 0) {
            Object[] temp = new Object[list.length * 2];
            System.arraycopy(list, 0, temp, 0, list.length);
            return temp;
        }
        return new Object[5];
    }

    @Override
    public void findAll() {
        for (Object o : list) {
            if (o != null) {
                System.out.println(o);
            }
        }
    }

    @Override
    public String toString() {
        return "NewListImpl{" +
                "list=" + Arrays.toString(list);
    }
}
